package com.example.tests;

import static com.example.tests.ContactDataGenerator.loadsContactsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactModificationTest extends TestBase {
	
	@DataProvider
	public static Iterator<Object[]> contactFromFile() throws IOException {
		return wrapContactsForDataProvider(loadsContactsFromXmlFile(new File("contacts.xml"))).iterator();
	}
	
	@Test(dataProvider = "contactFromFile")
	public void mofifySomeContact(ContactData contact) throws Exception{
		// save old state
		SortedListOf<ContactData> oldList = app.getContactHelper().getContacts();
		
		Random rnd = new Random();
		int index = rnd.nextInt(oldList.size() - 1);

		// action		
		app.getContactHelper().modifyContact(index, contact);

		// save new state
		SortedListOf<ContactData> newList = app.getContactHelper().getContacts();

		// compare states
		assertThat(newList, equalTo(oldList.withAdded(contact).without(index)));
	}

}
